const { JSDOM } = require('jsdom');

const { window } = new JSDOM('');
const fs = require('fs-extra');
const $ = require('jquery')(window);

// console.log($);

const prettier = require('prettier');

const isUpperCase = (character) => {
    return character === character.toUpperCase();
};

const allMethods = Object.keys($.fn).filter(
    (method) => typeof $.fn[method] === 'function'
);

const allParentMethods = Object.keys($).filter(
    (method) => typeof $[method] === 'function'
);

let finalMethods = [...allMethods, ...allParentMethods];

finalMethods = finalMethods
    .filter(
        (method) => !isUpperCase(method.charAt(0)) && method.charAt(0) !== '_'
    )
    .sort();

const content = `${
    '// This file is autogenerated. Do not modify manually \n' +
    'module.exports = '
}${JSON.stringify(finalMethods)}`;

const formattedContent = prettier.format(content, {
    parser: 'babel',
    tabWidth: 4,
    singleQuote: true,
});

fs.outputFile('src/all-methods.js', formattedContent).catch((err) => {
    console.error(err);
});
